package com.shop.dao.impl;

import com.shop.dao.IOrderDao;
import com.shop.entity.Orders;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class OrderDaoImpl extends BaseDao implements IOrderDao {

    private static Map<String,String> map = new HashMap<>();

    static {
        map.put("o_id","oid");
        map.put("u_id","uid");
        map.put("a_id","aid");
        map.put("o_count","ocount");
        map.put("o_time","otime");
        map.put("o_state","ostate");
    }

    /**
     * 生成订单
     * @param orders
     * @return
     */
    @Override
    public int insert(Orders orders) {
        String sql = "insert into orders values (?,?,?,?,?,?)";
        return update(sql,orders.getOid(),orders.getUid(),orders.getAid(),orders.getOcount(),orders.getOtime(),orders.getOstate());
    }

    /**
     * 查询用户所有订单
     * @param uid
     * @return
     */
    @Override
    public List<Orders> findAllOrderByUid(int uid) {
        String sql = "select o_id,u_id,a_id,o_count,o_time,o_state from orders where u_id = ?";
        return queryForList(Orders.class,sql,map,uid);
    }

    /**
     * 根据订单id查询指定订单
     * @param oid
     * @return
     */
    @Override
    public Orders findAllOrderByOid(String oid) {
        String sql = "select o_id,u_id,a_id,o_count,o_time,o_state from orders where o_id = ?";
        return queryForOne(Orders.class,sql,map,oid);
    }

    /**
     * 改变订单状态
     * @param oid
     * @param status
     * @return
     */
    @Override
    public int changeStatus(String oid, int status) {
        String sql = "update orders set o_state = ? where o_id = ?";
        return update(sql,status,oid);
    }

}
